package com.sk.skplanet.commute.parser;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.text.html.parser.Parser;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.sk.skplanet.commute.vo.GetStationByRouteVo;

public class GetStationByRouteParser extends SeoulBusAPIParser {
	private List<GetStationByRouteVo> result;
	
	public GetStationByRouteParser(String rawdata) {
		super(rawdata);
	}
	
	@Override
	public List<GetStationByRouteVo> parse() {
		result = new ArrayList<GetStationByRouteVo>();
		
		try {
			XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
			XmlPullParser parser = parserCreator.newPullParser();

			parser.setInput(new StringReader(rawData));
			parser.next();
			
			int parserEvent = parser.getEventType();

			String tagName = "";
			GetStationByRouteVo objectToAdd = null;

			while (parserEvent != XmlPullParser.END_DOCUMENT) {
				switch (parserEvent) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					tagName = parser.getName();
					if(tagName.equals("itemList"))
						objectToAdd = new GetStationByRouteVo();
					break;
				case XmlPullParser.END_TAG:
					if(parser.getName().equals("itemList")) {
						result.add(objectToAdd);
						objectToAdd = null;
					}
					break;
				case XmlPullParser.TEXT:
					String text = parser.getText();
					
					if(objectToAdd != null) {
						if(tagName.equals("beginTm"))
							objectToAdd.setBeginTm(text);
						else if(tagName.equals("busRouteId"))
							objectToAdd.setBusRouteId(text);
						else if(tagName.equals("busRouteNm"))
							objectToAdd.setBusRouteNm(text);
						else if(tagName.equals("direction"))
							objectToAdd.setDirection(text);
						else if(tagName.equals("gpsX"))
							objectToAdd.setGpsX(text);
						else if(tagName.equals("gpsY"))
							objectToAdd.setGpsY(text);
						else if(tagName.equals("lastTm"))
							objectToAdd.setLastTm(text);
						else if(tagName.equals("routeType"))
							objectToAdd.setRouteType(text);
						else if(tagName.equals("section"))
							objectToAdd.setSection(text);
						else if(tagName.equals("seq"))
							objectToAdd.setSeq(text);
						else if(tagName.equals("station"))
							objectToAdd.setStation(text);
						else if(tagName.equals("stationNm"))
							objectToAdd.setStationNm(text);
						else if(tagName.equals("stationNo"))
							objectToAdd.setStationNo(text);
						else if(tagName.equals("fullSectDist"))
							objectToAdd.setFullSectDist(text);
						else if(tagName.equals("trnstnid"))
							objectToAdd.setTrnstnid(text);
					}

					break;
				}

				parserEvent = parser.next();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}
}
